function Alive(){
}

Alive.prototype.draw = function(time){
	var part =  (time - game.lastLogicTime) / game.logicPeriod;
	var x = this.x - this.width/2 + this.move.x * part;
	var y = this.y - this.height/2 + this.move.y * part;

	var animation;
	if (this.move.x == 0 && this.move.y == 0){
		animation = 'standing';
	}
	else if (Math.abs(this.move.x) >= Math.abs(this.move.y)){
		animation = this.move.x > 0 ? 'right': 'left';
	} else {
		animation = this.move.y > 0? 'down': 'up';
	}
	this.animation.draw(x, y, animation, time);
}

Alive.prototype.logic = function(){
	this.x += this.move.x
	this.y += this.move.y
}

Alive.prototype.checkPosition = function(){
	//check limits
	var x = this.x + this.move.x;
	var y = this.y + this.move.y;
	x = Math.min(x, game.canvas.width - this.width/2); //x max
	x = Math.max(x, this.width/2); //x min
	y = Math.min(y, game.canvas.height - this.height/2); //y max
	y = Math.max(y, this.height/2); //y min
	this.move.x = x - this.x;
	this.move.y = y - this.y;
}

Alive.prototype.colides = function(second){
	if (Math.abs(this.x - second.x) < ((this.width + second.width)/2)){
		if (Math.abs(this.y - second.y) < ((this.height + second.height)/2)){
			return true;
		}
	}
	return false;
}



function Spider(){
	this.x = 0;
	this.y = 0;
	this.move = {x:0, y:0};
	this.animation = new Animation(this.width, this.height, loader.loadImage('img/spider.png'), game.context, {
		left: {row: 1},
		right: {row: 3},
		up: {row: 0},
		down: {row: 2},
		standing: {row: 2, frames: 1},
		death: {row: 4}
	}, 50 );
}

Spider.prototype = new Alive();
Spider.prototype.width = 64;
Spider.prototype.height = 64;
Spider.prototype.speed = 5;

Spider.prototype.logic = function(){
	Alive.prototype.logic.call(this);
	var xDiff = game.hero.x - this.x;
	var yDiff = game.hero.y - this.y;
	this.move.x = Math.max(Math.min(xDiff, this.speed), -this.speed);
	this.move.y = Math.max(Math.min(yDiff, this.speed), -this.speed);
	this.checkPosition();
}


function Hero(){
	this.x = 0;
	this.y = 0;
	this.move = {x:0, y:0};
	this.animation = new Animation(this.width, this.height, loader.loadImage('img/hero.png'), game.context, {
		left: {row: 1},
		right: {row: 3},
		up: {row: 0},
		down: {row: 2},
		standing: {row: 2, frames: 1},
		death: {row: 4}
	}, 50 );
};

Hero.prototype = new Alive();
Hero.prototype.width = 64;
Hero.prototype.height = 64;
Hero.prototype.speed = 10;

Hero.prototype.logic = function(){
	Alive.prototype.logic.call(this);
	this.move.y = this.speed*(keyboard.down - keyboard.up);
	this.move.x = this.speed*(keyboard.right - keyboard.left);
	this.checkPosition();
}

function Shot(x, y, dir){
	this.x = x;
	this.y = y;
	var len = Math.sqrt(dir.x * dir.x + dir.y * dir.y );
	this.move = {
		x: dir.x / len * this.speed,
		y:dir.y / len * this.speed
	};
	this.animation = new Animation(this.width, this.height, loader.loadImage('img/bullet.png'), game.context, {
		main: {row: 0}
	}, 50);
}
Shot.prototype = new Alive();
Shot.prototype.width = 16;
Shot.prototype.height = 19;
Shot.prototype.speed = 20;

Shot.prototype.logic = function(){
	Alive.prototype.logic.call(this);
	if (this.x<0 || this.y<0 || this.x>game.canvas.width || this.y>game.canvas.height){
		var i = game.objects.indexOf(this);
		game.objects.splice(i, 1);
	}
}
Shot.prototype.draw = function(time){
	var part =  (time - game.lastLogicTime) / game.logicPeriod;
	var x = this.x - this.width/2 + this.move.x * part;
	var y = this.y - this.height/2 + this.move.y * part;
	this.animation.draw(x, y, 'main', time);
}



var game = {

	objects: [],
	logicPeriod: 40,
	lastLogicTime : null,

	init: function(){
		this.canvas = document.getElementById('foreground');
		this.context = this.canvas.getContext('2d');
		this.hero = new Hero();
		this.spider = new Spider();
		this.objects.push(this.hero);
		this.objects.push(this.spider);
		this.canvas.addEventListener('mousedown', function(e){
			sound.play('shot');
			var rect = game.canvas.getBoundingClientRect();
			var mouseX = e.clientX - rect.left;
			var mouseY = e.clientY - rect.top;
			game.objects.push(new Shot(game.hero.x, game.hero.y, {x: mouseX - game.hero.x,y: mouseY -game.hero.y}));
		});
	},
	moveAll: function(){
		this.lastLogicTime = performance.now();
		for (var i=0; i<this.objects.length; ++i){
			this.objects[i].logic();
		}
		for (var i=0; i<this.objects.length; ++i){
			if (this.objects[i] !== this.hero && this.objects[i].colides(this.hero)){
				sound.play('hurt');
			}
		}

	},

	drawAll: function(time){
		this.context.clearRect ( 0 , 0 , this.canvas.width , this.canvas.height );
		for (var i=0; i<this.objects.length; ++i){
			this.objects[i].draw(time);
		}
	}
}

function drawingLoop(nowTime){
	game.drawAll(nowTime);
	requestAnimationFrame(drawingLoop, game.canvas);
}

function logicLoop(){
	game.moveAll();
}

